<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;
class CategoryController extends PublicController {
	
	
	//商品列表
	public function listAction(){
		
		$all=I('get.all');
		if($all=='all'){
			session('params',null); // 删除params
		}
		
		//读取热销推荐
		$hlist=$this->getshoplist('hot',$count=5);
		$this->assign('hlist',$hlist);
		
		//读取分类树
		$tree=$this->gettree(0);
		$this->assign('tree',$tree);
		
		//品牌读取
		$blist=$this->brandselect();
		$this->assign('blist',$blist);// 赋值数据集
		
		//价格区间
		$plist=$this->getprice(0,100,8);
		$this->assign('plist',$plist);// 赋值数据集

		$calist=$this->pagelist();
		
		$this->assign('list',$calist['list']);

		
		$this->assign('page',$calist['page']);
		
		$this->assign('params',$calist['params']);

		$this->view();
	}
	
	
	//ajax
/*	public function ajaxAction(){
		
		$calist=$this->pagelist();
		
		$arry=array('list'=>$calist['list']);
		
		echo $arry;
		
	}*/

	/*
	查找分类下的所有子分类
	*/
	public function getchinkid($id,$array=array()){
		$Model=D("Category"); // 实例化对象			
		$array[]=$id;
		$Tlist=$Model->where(' parentid='.$id)->select();
		foreach($Tlist as $key=>$val){
			$array=$this->getchinkid($val['catid'],$array);
			}
			return $array;
	}
		
	
	//品牌读取
	public function brandselect(){
		$Brand=D("Brand"); // 实例化role对象
		$brand=$Brand->select();
		return $brand;
	}
	
	
	public function pagelist(){
       
		$Model=D('Goods');
		
		$params=$this->getparams();
		
		//print_r($params);
		//echo '<br/>';

		//取到分类id
		$catid=empty($params['catid'])?0:$params['catid'];
		
		//取到品牌
		$bid=empty($params['bid'])?0:$params['bid'];
		
		//取价格区间
		$lower=empty($params['lower'])?0:$params['lower'];
		$high=empty($params['high'])?0:$params['high'];
		
		//取不同类型的商品
		$type=empty($params['type'])?0:$params['type'];
		if(!empty($type)){
			
			switch($type){
				case 'new':
					$map['new']  = array('eq',1);	
				break;
				case 'hot':
					$map['hot']  = array('eq',1);	
				break;
				case 'recommend':
					$map['recommend']  = array('eq',1);	
				break;
				default;
			}	
		}
		
		
		//分类
		if($catid != 0){
			$list=$this->getchinkid($catid);
			
			$map['catid']  = array('in',$list);
		}
		
				//品牌
		if($bid != 0){
			$map['bid']  = $bid;
		}
		
		//价格区间
		if($high!=0){
			$map['price']=array(array('gt',$lower),array('lt',$high)); 
		}else{
			$map['price']=array(array('gt',$lower)); 
		}
		
		//排序
		$fields=I('get.fields','id');
		$sort=I('get.sort','desc');

		
		$count      = $Model->where($map)->count();// 查询满足要求的总记录数
		//echo $Model->getlastsql();
		$Page       = new \Think\Page($count,8);// 实例化分页类 传入总记录数和每页显示的记录数		
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','尾页');		
		$Page->setConfig('header','<li><span class="rows">共 %TOTAL_ROW% 条记录</span></li>');		
		$Page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %PAGES%');		
		$list = $Model->where($map)->order(" $fields $sort ")->limit($Page->firstRow.','.$Page->listRows)->select();
		$show       = $Page->show();// 分页显示输出
		
		$p=I('get.p',0,int);
		if(empty($p)){
			$sort=$sort=='desc'?'asc':'desc';
		}
		$params['sort']=$sort;
		
		return array('list'=>$list,'page'=>$show,'params'=>$params);
		
	}
	
	//价格区间
	public function getprice($lower=0,$high=100,$count=6){		
		$price=array();		
		$price[0]['lower']=$lower;
		$price[0]['high']=$high;
		$price[0]['between']=$lower.'-'.$high;
		for($i=1;$i<$count;$i++){
			$price[$i]['lower']=$high*$i;
			$price[$i]['high']=$high*($i+1);
			$price[$i]['between']=$high*$i.'-'.$high*($i+1);	
		}
		$price[$count-1]['lower']=$high*($count-1);
		$price[$count-1]['high']=0;
		$price[$count-1]['between']=$high*($count-1).'以上';
		return $price;		
	}
	

	
	
	
}